﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyMainSite2.Data.Repositories
{
	public class AdminUserRepository : IAdminUserRepository
	{
		#region Public Methods

		public bool ValidateUserNameAndPassword(string userName, string password)
		{
			if (String.IsNullOrWhiteSpace(userName))
				throw new ArgumentException("userName");

			if (String.IsNullOrWhiteSpace(password))
				throw new ArgumentException("password");

			var context = new MyMainSite2Entities();

			var isValidUser = (from u in context.tblAdminUsers
							   where u.UserName == userName &&
									 u.Password == password
							   select u).Count() > 0;

			return isValidUser;
		}

		public tblAdminUser GetByUserName(string userName)
		{
			if (String.IsNullOrWhiteSpace(userName))
				throw new ArgumentException("userName");

			var context = new MyMainSite2Entities();

			userName = userName.Trim();

			var user = (from u in context.tblAdminUsers
						where u.UserName == userName
						select u).FirstOrDefault();

			return user;
		}

		public bool UpdateUser(tblAdminUser user)
		{
			if (user == null)
				throw new ArgumentNullException("user");

			bool result = false;

			try
			{
				var context = new MyMainSite2Entities();

				var userToUpdate = (from u in context.tblAdminUsers
									where u.AdminUserId == user.AdminUserId
									select u).FirstOrDefault();

				userToUpdate.FirstName = user.FirstName;
				userToUpdate.LastName = user.LastName;

				context.SaveChanges();

				result = true;
			}
			catch (Exception ex)
			{
			}

			return result;
		}

		#endregion
	}
}
